package com.microstation.controller.system;

import com.microstation.common.Result;
import com.microstation.dto.UploadDTO;
import com.microstation.enums.ResultEnum;
import com.microstation.util.FilePathUtil;
import com.microstation.util.IdGenerator;
import com.microstation.util.OSSFilesUtil;
import com.microstation.util.StringUtil;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件上传
 */
@RestController
@RequestMapping(value = "/file")
public class FileController extends BaseController {
    @Autowired
    private OSSFilesUtil ossFilesUtil;


    /**
     * 图片上传
     *
     * @param path
     * @param files
     * @return
     */
    @PostMapping(value = "/upload")
    public Result upload(String path, @RequestParam(value = "file", required = false) MultipartFile[] files, HttpServletRequest request) {
        Result result = new Result();
        if (StringUtil.isEmpty(path)) {
            result.setHasError(true);
            result.setResultEnum(ResultEnum.EMPTY, "文件地址为空");
            return result;
        }
        List<UploadDTO> fileList = new ArrayList<UploadDTO>();
        if (files != null) {
            try {
                UploadDTO item = null;
                for (int i = 0; i < files.length; i++) {
                    if (!files[i].isEmpty()) {
                        String filePath = request.getSession().getServletContext().getRealPath(File.separator + "upload");
                        String forderName = FilePathUtil.getForderName(path);
                        String fileId = IdGenerator.getPrimaryKey();
                        String thumbName = FilePathUtil.THUMBNAIL_NAME_SUFFIX + fileId;
                        // 临时文件路径
                        String thumbPath = forderName + File.separator + thumbName + ".png";
                        String realPath = forderName + File.separator + fileId + ".png";

                        /*上传到本地*/
                        File mbFile = new File(filePath + File.separator + realPath);
                        mbFile.getParentFile().mkdirs();
                        files[i].getInputStream();

                        File thumbnailFile = new File(filePath + File.separator + thumbPath);
                        Thumbnails.of(mbFile).size(200, 200).outputFormat("png").toFile(thumbnailFile);
                        item = new UploadDTO("upload" + File.separator + realPath, fileId);
                        fileList.add(item);
                    } else {
                        result.setHasError(true);
                        result.setResultEnum(ResultEnum.EMPTY);
                        return result;
                    }
                }
                result.setHasError(false);
                result.setRows(fileList);
                result.setResultEnum(ResultEnum.SUCCESS);
            } catch (Exception e) {
                result.setHasError(true);
                result.setError(e.getMessage());
            }
        } else {
            result.setHasError(true);
            result.setResultEnum(ResultEnum.EMPTY);
        }
        return result;
    }


    /**
     * 图片上传至OSS
     *
     * @param path
     * @param files
     * @return
     */
    @PostMapping(value = "/uploadOSS")
    public Result uploadOSS(String path, @RequestParam(value = "file", required = false) MultipartFile[] files, HttpServletRequest request) {
        Result result = new Result();
        if (StringUtil.isEmpty(path)) {
            result.setHasError(true);
            result.setResultEnum(ResultEnum.EMPTY, "文件地址为空");
            return result;
        }
        List<UploadDTO> fileList = new ArrayList<UploadDTO>();
        if (files != null) {
            try {
                UploadDTO item = null;
                for (int i = 0; i < files.length; i++) {
                    if (!files[i].isEmpty()) {
                        String filePath = request.getSession().getServletContext().getRealPath(File.separator + "upload");
                        String forderName = FilePathUtil.getForderNameOSS(path);
                        String fileId = IdGenerator.getPrimaryKey();
                        String thumbName = FilePathUtil.THUMBNAIL_NAME_SUFFIX + fileId;
                        // 临时文件路径
                        String thumbPath = forderName + "/" + thumbName + ".png";
                        String realPath = forderName + "/" + fileId + ".png";
                        /*上传到oss*/
                        try {
                            ossFilesUtil.uploadDocumentByFile("upload/" + realPath, files[i].getInputStream());
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }

                        item = new UploadDTO("upload" + "/" + realPath, fileId);
                        fileList.add(item);
                    } else {
                        result.setHasError(true);
                        result.setResultEnum(ResultEnum.EMPTY);
                        return result;
                    }
                }
                result.setHasError(false);
                result.setRows(fileList);
                result.setResultEnum(ResultEnum.SUCCESS);
            } catch (Exception e) {
                result.setHasError(true);
                result.setError(e.getMessage());
            }
        } else {
            result.setHasError(true);
            result.setResultEnum(ResultEnum.EMPTY);
        }
        return result;
    }
}
